package controller

import (
	"errors"

	"github.com/gin-gonic/gin"
)

var (
	// ErrorAuth 认证错误提示
	ErrorAuth = errors.New("please add token: 'Authorization: Bearer xxxx'")
)

// AuthRequired 认证中间件
func AuthRequired() gin.HandlerFunc {
	return func(c *gin.Context) {
		authorization := c.Request.Header.Get("Authorization")
		if authorization == "" {
			c.AbortWithError(400, ErrorAuth)
		}
		c.Next()
	}
}
